<?php
namespace app\index\controller;
use \think\Controller;
use \think\Loader;
use app\index\model\EmailCheck;
use app\index\model\User;
use phpmailer\Mail;
use think\Request;
use think\captcha\Captcha;
require_once("../vendor/qqConnect/API/qqConnectAPI.php");
class Login extends Common
{
    public function test()
    {

        $content = <<<xbs
        <div></div><div><includetail><div><br></div><div><br></div><table style='margin: auto; border-radius: 3px; border: 1px solid rgb(221, 221, 221); border-image: none; width: 600px; height: auto; text-align: left; color: rgb(85, 85, 85); overflow: hidden; font-family: "Helvetica Neue Regular",Helvetica,Arial,Tahoma,"Microsoft YaHei","San Francisco","微软雅黑","Hiragino Sans GB",STHeitiSC-Light; font-size: 12px; -ms-word-break: break-all; -ms-word-wrap: break-word;' border="0" cellspacing="0" cellpadding="0"> <tbody style="margin: 0px; padding: 0px;"> <tr style="margin: 0px; padding: 0px; height: 60px; background-color: rgb(57, 61, 73);"> <td style="margin: 0px; padding: 0px;"> <div style="margin: 0px 0px 0px 30px; padding: 0px; color: rgb(94, 181, 118);"><a style="margin: 0px; padding: 0px; color: rgb(94, 181, 118); font-size: 14px; text-decoration: none;" href="http://fly.layui.com/" target="_blank">Fly社区 - Layui前端框架官方社区</a></div> </td> </tr> <tr style="margin: 0px; padding: 0px;"> <td style="margin: 0px; padding: 30px;"> <p style="margin: 0px 0px 10px; padding: 0px; line-height: 20px;"> 你好，<em style="font-weight: 700;">学并思</em>童鞋，请在30分钟内重置您的密码： </p> <div> <a style="margin: 0px 15px 0px 0px; padding: 0px 15px; height: 32px; color: rgb(255, 255, 255); line-height: 32px; text-decoration: none; display: inline-block; background-color: rgb(0, 158, 148);" href="http://fly.layui.com/user/forget?token=d99fab4da9632c258bd4e9f3741a83f0&amp;key=email&amp;value=113664000%40qq.com&amp;_=%60b%3C.JfRO%5DD%3BB%26%25Ph" target="_blank">立即重置密码</a> </div> <p style="padding: 10px; line-height: 20px; font-size: 12px; margin-top: 20px; background-color: rgb(242, 242, 242);"> 如果该邮件不是由你本人操作，请勿进行激活！否则你的邮箱将会被他人绑定。 </p> </td> </tr> <tr style="margin: 0px; padding: 0px; height: 35px; text-align: center; color: rgb(153, 153, 153); background-color: rgb(250, 250, 250);"> <td style="margin: 0px; padding: 0px;">系统邮件，请勿直接回复。</td> </tr> </tbody> </table></includetail></div>

xbs;

        $res = Mail::send('rt@qq.com','测dfgdfg试', $content);

        var_dump($res);
    }
    public function index()
    {
    	$this->assign('title',"登录");
    	return $this->fetch();
    }
    public function checklogin()
    {
    	// 接数据 
    	$data = input('post.');
    	// 加载验证器
    	$validate = Loader::validate('reg');
    	// 判断数据是否通过
		if(!$validate->scene('login')->check($data)){
		    $this->error($validate->getError());
		}

		if(strpos($data['loginName'],'@')){
			$email = $data['loginName'];
			$user = User::get(['email' => $email,'password'=>md5($data['password'])]);
		}else{
			$phone = $data['loginName'];
			$user = User::get(['phone' => $phone,'password'=>md5($data['password'])]);
		}

		if($user){
            if($user->status=='1'){
                $this->error('帐号被停用，请联系管理员');
            }
			session('uid',$user->uid);
			session('username',$user->username);
			session('face',$user->face);

			$this->success('登录成功');
		}else{
			$this->error('登录名或者密码错误');
		}
    }

    public function loginout()
    {
    	session(null);
    	header("Location:".url('index/login/index'));exit;
    	// $this->success("退出成功");
    }

    public function forget()
    {
        if(Request::instance()->isAjax()){

            $data = input('post.');

            $ca = new Captcha();

            if(!$ca->check($data['captcha'])){
                $this->error("验证码错误");
            };

            $user = User::get(['email'=>$data['email']]);

            if(!$user){
                 $this->error("该用户不存在");
            }

            $content = $this->reset_pass_tpl($user);

            $res = Mail::send('er@qq.com','密码重置', $content);

            if($res){
                $this->success('邮件发送成功，请打开邮箱查看');
            }else{
                $this->error('邮件发送失败');
            }

        }else{
            return $this->fetch();
        }
    }

    public function reset_pass_tpl($user)
    {
        $token = md5(uniqid().mt_rand(1,20));

        $url = "http://www.zhibin.com".url('index/login/resetpass')."?email=".$user->email."&token=".$token;

        EmailCheck::create(['email'=>$user->email,'token'=>$token,'ctime'=>time()]);

        $content = <<<xbs
<div></div><div><includetail><div><br></div><div><br></div><table style='margin: auto; border-radius: 3px; border: 1px solid rgb(221, 221, 221); border-image: none; width: 600px; height: auto; text-align: left; color: rgb(85, 85, 85); overflow: hidden; font-family: "Helvetica Neue Regular",Helvetica,Arial,Tahoma,"Microsoft YaHei","San Francisco","微软雅黑","Hiragino Sans GB",STHeitiSC-Light; font-size: 12px; -ms-word-break: break-all; -ms-word-wrap: break-word;' border="0" cellspacing="0" cellpadding="0"> <tbody style="margin: 0px; padding: 0px;"> <tr style="margin: 0px; padding: 0px; height: 60px; background-color: rgb(57, 61, 73);"> <td style="margin: 0px; padding: 0px;"> <div style="margin: 0px 0px 0px 30px; padding: 0px; color: rgb(94, 181, 118);"><a style="margin: 0px; padding: 0px; color: rgb(94, 181, 118); font-size: 14px; text-decoration: none;" href="http://fly.layui.com/" target="_blank">老司机论坛</a></div> </td> </tr> <tr style="margin: 0px; padding: 0px;"> <td style="margin: 0px; padding: 30px;"> <p style="margin: 0px 0px 10px; padding: 0px; line-height: 20px;"> 你好，<em style="font-weight: 700;">{$user->username}</em>童鞋，请在30分钟内重置您的密码： </p> <div> <a style="margin: 0px 15px 0px 0px; padding: 0px 15px; height: 32px; color: rgb(255, 255, 255); line-height: 32px; text-decoration: none; display: inline-block; background-color: rgb(0, 158, 148);" href="{$url}" target="_blank">立即重置密码</a> </div> <p style="padding: 10px; line-height: 20px; font-size: 12px; margin-top: 20px; background-color: rgb(242, 242, 242);"> 如果该邮件不是由你本人操作，请勿进行激活！否则你的邮箱将会被他人绑定。 </p> </td> </tr> <tr style="margin: 0px; padding: 0px; height: 35px; text-align: center; color: rgb(153, 153, 153); background-color: rgb(250, 250, 250);"> <td style="margin: 0px; padding: 0px;">系统邮件，请勿直接回复。</td> </tr> </tbody> </table></includetail></div>

xbs;
        return $content;

    }

    public function resetpass()
    {
        $data =  input('');

        var_dump($data);
    }

    public function qqlogin()
    {
        $qc = new \QC();

        $qc->qq_login();
    }

    public function qqreturn()
    {
        $qc = new \QC();
        $qc->qq_callback();
        $openid = $qc->get_openid();

        $user = User::get(['openid'=>$openid]);

        if($user){
            session('uid',$user->uid);
            session('username',$user->username);
            session('face',$user->face);


            header("Location:".url('index/user/set'));

            // $this->success('qq登录成功',url('index/user/set'));

        }else{
            
            $qc = new \QC();
            $user = $qc->get_user_info();

            $data['username'] = $user['nickname']; 
            $data['sex'] = $user['gender']; 
            $data['city'] = $user['city']; 
            $data['openid'] = $openid; 
            $data['face'] = $this->getqqface($user['figureurl_2']);
            $data['ctime'] = time();

            $user = User::create($data);

            if($user){
                session('uid',$user->uid);
                session('username',$user->username);
                session('face',$user->face);
                $this->success('qq登录成功',url('index/user/set'));
            }else{
                $this->error('登录失败',url('index/login/index'));
            }
        }

    }

    public function getqqface($url)
    {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_TIMEOUT, 5);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        //头部要不要
        curl_setopt($ch, CURLOPT_HEADER, 0);

        $data = curl_exec($ch);

        curl_close($ch);

        $dir = "uploads/".date('Ymd');

        is_dir($dir) || mkdir($dir);

        $face = $dir.'/'.uniqid().".png";

        file_put_contents($face, $data);

        return $face;
    }
}
